<template>
    <div class="agency">
        <div class="carousel">
            <div class="head">
                <van-icon name="arrow-left"
                size="0.8rem"
                class="back-icon"
                @click="goback()" /> 
                <svg class="icon agency-icon love" aria-hidden="true" v-show="status === 2 " @click.stop="dianzan()">
                  <use xlink:href="#icon-shoucang1"></use>
                </svg>
                <svg class="icon agency-icon love" aria-hidden="true" v-show="status === 1 " @click.stop="dianzan()">
                    <use xlink:href="#icon-aixin1"></use>
                </svg>
            </div>
            <div class="no-pet" v-if="!petList.length">
              <p>该机构暂无发布宠物</p>         
            </div>
            <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white" v-if="petList.length">
                <van-swipe-item v-for="(pet,index) in petList.slice(0,4)" :key="index">
                    <van-image :src="pet.profile" 
                        alt="" class="carousel-img">
                        <template v-slot:loading>
                            <van-loading type="spinner" size="20" />
                        </template>
                    </van-image>    
                </van-swipe-item>
            </van-swipe>
        </div>
        <div class="agency-content">
            <div class="agency-info">
                <div class="agency-name">
                  <p>{{agencyInfo.real_name}}</p>
                  <svg class="icon chat-icon" aria-hidden="true">
                    <use xlink:href="#icon-xinxi-01"></use>
                  </svg>
                </div>
                <p> <span class="open-color">Open:</span>
                    {{agencyInfo.open}}</p>
                <p><van-icon name="location-o"/> {{agencyInfo.local}}</p>
                <p><van-icon name="phone-o" /> {{agencyInfo.tel}}</p>
                <p><van-icon name="envelop-o" /> {{agencyInfo.mail}}</p>
            </div>
            <div class="agency-pet" v-if="petList.length">
                <h3>宠物列表</h3>
                <div class="pet-list">
                    <div v-for="(pet,index) in petList" :key="index" class="pet-list-item">
                        <img :src="pet.profile" 
                         alt="" class="pet-img" @click="gotoPet(pet.id)"> 
                    </div>
                    
                </div>
            </div>
            <div class="introduce">
                <h3>机构介绍</h3>
                <p class="introduce-content">{{agencyInfo.detail}}</p>
            </div>
            <div class="agency-comment">
                <h3>评论区
                    <span class="commentList-count">({{commentList.length}})</span>
                </h3>
                <div class="comment-list">
                    <div v-for="(commentItem,index) in commentList" :key = "index" class="comment-item">
                        <div class="comment-info">
                            <div class="user-info" v-if="commentItem.userData">
                              <!-- <van-image v-if="privateInfo.profile" :src="privateInfo.profile" alt="" class="face-img">
                    <template v-slot:loading>
                        <van-loading type="spinner" size="20" />
                    </template>
                </van-image> -->
                                <van-image v-if="commentItem.userData.profile" :src="commentItem.userData.profile" alt="" class="user-img"  >
                                <template v-slot:loading>
                                    <van-loading type="spinner" size="20" />
                                </template>
                                </van-image>
                                <van-image src="" alt="" class="user-img"  v-else>
                                <template v-slot:loading>
                                    <van-loading type="spinner" size="20" />
                                </template>
                                </van-image>
                                <pre>{{commentItem.userData.uname}}</pre>
                            </div>     
                            <div class="user-info" v-if="!commentItem.userData">
                                <van-image src="" alt="" class="user-img"  >
                                <template v-slot:loading>
                                    <van-loading type="spinner" size="20" />
                                </template>
                                </van-image>
                                <pre class="comment-uname">该用户已注销</pre>
                            </div>                        
                            <p class="comment-time">{{date(commentItem.create_time)}}</p>
                        </div>
                        <p class="comment-content">{{commentItem.comment_content}}</p>                        
                    </div>

                </div>
            </div>
        </div>
        <div class="write-comment">
            <p-button @click.native="textShow()"><van-icon name="comment-o" size="0.5rem"/>写评论...</p-button>
        </div>
        <van-popup v-model="popup" position="bottom" :style="{ height: '20%' }" >
            <textarea name="" id="" cols="35" rows="5" placeholder="写评论" v-model="textArea"></textarea>
            <p class="send" @click.stop="sendText()">发送</p>
        </van-popup>
    </div>
</template>

<script>
import PButton from '@/components/button.vue'
import {getAgencyInfo, getpetInfo, getComment, getuserInfo, upSoloLike, updateComment} from '@/api.js'
import { Toast } from 'vant'
export default {
    components: {
            PButton
    },
    data() {
        return{
            agencyInfo:{},
            petList:[],
            commentList:[],
            status:'',
            popup:false,
            textArea:''
        }
    },
    created() {
        this.getAgencyInfo()
    },
    methods:{
        date(time) {
            var date = new Date(time);
            // console.log(date)
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            var d = date.getDate();
            var h = date.getHours();
            var mi = date.getMinutes();
            m = m > 9 ? m : '0' + m;
            return y + '-' + m + '-' + d + ' ' + h + ':' + mi;
        },
        async sendText() {
            let params = {comment_content: this.textArea, belong:this.$route.params.aid, user_id:JSON.parse(localStorage.getItem('user')), create_time:new Date()}           
            const {success} = await updateComment(params)
            if (success) {
                Toast({
                message: '已发送',
                icon: 'passed',
                })
            }
            this.getComment()
            this.popup = false
            this.textArea = ''
        },
        textShow(){
            this.popup = true
        },
        async dianzan() {
            if(this.status === 1) {
                this.status = 2
            }else{
                this.status = 1
            }
            let params = {aid:this.$route.params.aid,user_id:JSON.parse(localStorage.getItem('user')),status:this.status}
            const { data } =await upSoloLike(params)
        },
        gotoPet(id){
            this.$router.push('/pet/'+id)
        },
        goback(){
            this.$router.go(-1)
        },
        async getAgencyInfo () {  
                let aid = this.$route.params.aid
                let params = {user_id:JSON.parse(localStorage.getItem('user'))}
                await getAgencyInfo(params).then(res => {
                    let agencyInfo =res.data
                    var index = agencyInfo.findIndex( (item) => {
                        return item.aid === aid
                    })
                    this.agencyInfo = agencyInfo[index]
                    this.status = agencyInfo[index].status                
                })
                let paramss = {aid:this.$route.params.aid}
                await getpetInfo(paramss).then(res => {
                    this.petList = res.data
                })
                this.getComment()                       
        },
        async getComment() {
            let paramss = {aid:this.$route.params.aid}
            const { data} = await getComment(paramss)
                this.commentList = data
                this.commentList.map(item => {
                this.getuserInfo(item)
                })  
        },
        async getuserInfo(item) {
            const userId = item.user_id
            let params = {mail:userId}
            try {
              const { data } = await getuserInfo(params)
            //对象身上已经有了该属性，但是视图层并没有更新该数据,用this.$set
            this.$set(item,'userData',data)  
            } catch (error) {              
            }
            
        }
    }
}
</script>
<style lang="less" scoped>
.agency {
    position: relative;
    padding-bottom: 1.6rem;
    .van-popup--bottom {
        padding:0.5rem;
        display: flex;
        align-items: flex-end;
        textarea {
            border-radius: 0.2rem;
            background-color: rgba(179, 176, 170, 0.24);
            margin-right: 0.1rem;
            flex:1;
            height: 100%;
            font-size: 0.4rem;
        }
        .send {
            color: #4895f2;
            font-weight: bold;
        }
    }
    
    .write-comment {
        height: 1.5rem;
        background-color: white;
        width: 100%;
        position: fixed;
        bottom: 0;
        left: 0;  
        padding:0 0.5rem;      
       /deep/ button {
            width: 100%;
            background-color: white;
            color: #4895f2;
            border: 0.02rem solid #4895f2;
            display: flex;
            align-items: center;
            justify-content: center;
            margin:0.3rem auto 0;
        }
    }
    .carousel{
        width: 100%;
        position: relative;
       
        .head{
            padding:0.5rem 0.25rem 0;
            width: 100%;
            height: 1.25rem;
            position: absolute;
            z-index: 999;
            display: flex;
            justify-content: space-between;
            .agency-icon{
                width: 0.7rem;
            }
            @keyframes hovermove {
              from {width: 0;}
              to {width: 0.7rem;}
            }
            .love{
              width: 0.7rem;
              animation: hovermove 0.3s;
            }
        }
        .no-pet{
          text-align: center;
          font-size:0.8rem;
          height:4rem;
          line-height: 5rem;
          color: #a09ea0;
        }
        .carousel-img{
           width: 100%; 
           height: 6rem;
        }
    }
    .agency-content{
        padding:0.3rem 0.5rem;
        .agency-info{
            color: #a09ea0;
            height: 4rem;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            margin-bottom: 0.5rem;
            .agency-name{
              color: #38383b;
              display: flex;
              justify-content: space-between;
              font-size: 0.6rem;
              font-weight: bold;
              align-items: center;
                .chat-icon {
                  width: 1rem;
                  height: 1rem;                 
                }                
            }
            .open-color{
                color: #4895f2;
            }
            .van-icon {
                vertical-align: middle;
            }
        }
        .agency-pet{  
            width: 100%;   
            margin-bottom: 0.5rem;
            .pet-list{
                margin-top: 0.35rem;
                overflow-y: hidden;
                display: flex;
                height: 2.5rem;
                .pet-list-item{
                    margin-right:0.13rem;
                    height: 2.5rem;
                    .pet-img{
                        height: 2.5rem;
                        border-radius: 0.13rem;
                    }
                }
            }
        }
        .introduce{
            .introduce-content{
                padding: 0.25rem 0;
                margin-bottom: 0.25rem;
                line-height: 0.5rem;
                font-size: 0.33rem;
            }
        }
        .agency-comment{
            .commentList-count{
                color: #a09ea0;
                font-weight: normal;
                font-size: 0.35rem;
            }
            .comment-list{
                .comment-item{
                    margin: 0.4rem 0;
                    font-size: 0.33rem;
                    .comment-info{
                        display: flex;
                        align-items: center;
                        justify-content: space-between;                       
                        .user-info{
                            display: flex;
                            align-items: center;
                            font-size: 0.38rem;
                            .user-img{
                            width: 1rem;
                            margin-right: 0.2rem;
                            border-radius:10rem;
                            
                            }
                            .comment-uname{
                                color: #a09ea0;
                            }
                        }   
                        .comment-time{
                            color: #a09ea0;
                        }                    
                    }
                    .comment-content{
                        margin-top: 0.2rem;
                        padding-left: 1.2rem;
                        line-height: 0.4rem;
                        width: 100%;
                        overflow:hidden;
                        word-wrap: break-word;
                        word-break: break-all;
                    }
                }
            }
        }
    }
}
</style>